/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import bean.*;
import com.opensymphony.xwork2.ActionSupport;
import dao.ReportAccess;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ShowReportAction extends ActionSupport {

    private Account user;
    private Account tech;
    private Category cate;
    private Complaint comp;
    private Deparment dep;
    private Priority pri;
    private List<ShowReportAction> listReport;
    private String dateSolve;
    private String numberOfDays;

    private List<Account> listTech;
    private List<Category> listCate;
    private List<Deparment> listDep;
    private List<Priority> listPri;

    public String getNumberOfDays() {
        return numberOfDays;
    }

    public void setNumberOfDays(String numberOfDays) {
        this.numberOfDays = numberOfDays;
    }

    public String getDateSolve() {
        return dateSolve;
    }

    public void setDateSolve(String dateSolve) {
        this.dateSolve = dateSolve;
    }

    public List<ShowReportAction> getListReport() {
        return listReport;
    }

    public void setListReport(List<ShowReportAction> listReport) {
        this.listReport = listReport;
    }

    public Account getUser() {
        return user;
    }

    public void setUser(Account user) {
        this.user = user;
    }

    public Account getTech() {
        return tech;
    }

    public void setTech(Account tech) {
        this.tech = tech;
    }

    public List<Account> getListTech() {
        return listTech;
    }

    public void setListTech(List<Account> listTech) {
        this.listTech = listTech;
    }

    public List<Category> getListCate() {
        return listCate;
    }

    public void setListCate(List<Category> listCate) {
        this.listCate = listCate;
    }

    public List<Deparment> getListDep() {
        return listDep;
    }

    public void setListDep(List<Deparment> listDep) {
        this.listDep = listDep;
    }

    public List<Priority> getListPri() {
        return listPri;
    }

    public void setListPri(List<Priority> listPri) {
        this.listPri = listPri;
    }

    public Category getCate() {
        return cate;
    }

    public void setCate(Category cate) {
        this.cate = cate;
    }

    public Complaint getComp() {
        return comp;
    }

    public void setComp(Complaint comp) {
        this.comp = comp;
    }

    public Deparment getDep() {
        return dep;
    }

    public void setDep(Deparment dep) {
        this.dep = dep;
    }

    public Priority getPri() {
        return pri;
    }

    public void setPri(Priority pri) {
        this.pri = pri;
    }

    public ShowReportAction(Account user, Account tech, Category cate, Complaint comp, Deparment dep, Priority pri) {
        this.user = user;
        this.tech = tech;
        this.cate = cate;
        this.comp = comp;
        this.dep = dep;
        this.pri = pri;
    }

    public ShowReportAction() {
    }

    public String execute() throws Exception {

        return SUCCESS;
    }

    public String showDetaildedReport() throws Exception {

        listReport = getSummaryReport(ReportAccess.getAllComplaint());

        return "showDetaildedReport";
    }

    public String showStatusReport() throws Exception {
        listReport = getSummaryReport(ReportAccess.getAllComplaint());
        return "showStatusReport";
    }

    public String showTechReport() throws Exception {
        listTech = ReportAccess.getAllAccount(4);

        if (comp != null && comp.getTechId() != 0) {
            listReport = getSummaryReport(ReportAccess.getAllComplaintByTechId(comp.getTechId()));
        } else {
            listReport = getSummaryReport(ReportAccess.getAllComplaint());

        }

        return "showTechReport";
    }

    public String showDeparmentReport() throws Exception {
        listDep = ReportAccess.getAllDeparment();
        if (comp != null && comp.getDepId() != 0) {
            listReport = getSummaryReport(ReportAccess.getAllComplaintByDepId(comp.getDepId()));
        } else {
            listReport = getSummaryReport(ReportAccess.getAllComplaint());

        }

        return "showDeparmentReport";
    }

    public String showCategoryReport() throws Exception {

        listCate = ReportAccess.getAllCategory();

        if (comp != null && comp.getCateId() != 0) {
            listReport = getSummaryReport(ReportAccess.getAllComplaintByCateId(comp.getCateId()));
        } else {
            listReport = getSummaryReport(ReportAccess.getAllComplaint());

        }
        return "showCategoryReport";
    }

    private List<ShowReportAction> getSummaryReport(List<Complaint> listC) {
        List<ShowReportAction> list = new ArrayList<ShowReportAction>();
        for (Complaint c : listC) {
            ShowReportAction ra = new ShowReportAction();
            ra.user = ReportAccess.findAcc(c.getAccId());
            ra.tech = ReportAccess.findAcc(c.getTechId());
            ra.comp = c;

            ra.dep = ReportAccess.findDep(ra.comp.getDepId());
            ra.pri = ReportAccess.findPri(c.getPriId());
            ra.cate = ReportAccess.findCate(c.getCateId());
            ra.dateSolve = getNumberOfDate(c.getDateOpen(), c.getDateClose());
            if (ra.dateSolve.equals("Not yet")) {
                ra.numberOfDays = getNumberOfDate(c.getCreateTime(), new Date());
            } else {
                ra.numberOfDays = "Done";
            }
            list.add(ra);
        }

        return list;

    }

    private String getNumberOfDate(Date dFirst, Date dAfter) {
        if (dAfter != null) {
            long day = (dAfter.getTime() - dFirst.getTime()) / (24 * 3600 * 1000);
            //      long time = ((c.getDateClose().getTime() - c.getDateOpen().getTime()) % (24 * 3600 * 1000)) / 1000;
            long time = ((new Date().getTime() - dFirst.getTime()) % (24 * 3600 * 1000)) / 1000;
            long gio = time / (3600);
            time = time % (3600);
            long fut = time / (60);
            time = time % (60);
            long giay = time;

            if (day == 0) {
                return time(gio) + " : " + time(fut) + " : " + time(giay);
            } else {
                return day + " days";
            }
        } else {
            return "Not yet";
        }
    }

    public static void main(String[] args) {
//        List<Complaint> listC = ReportAccess.getAllComplaint();
//        ShowReportAction r = new ShowReportAction();
//        r.listReport = new ArrayList<>();
//        for (Complaint c : listC) {
//            ShowReportAction ra = new ShowReportAction();
//            ra.user = ReportAccess.findAcc(c.getAccId());
//            ra.tech = ReportAccess.findAcc(c.getTechId());
//            ra.comp = c;
//            ra.dep = ReportAccess.findDep(c.getDepId());
//            ra.pri = ReportAccess.findPri(c.getPriId());
//            ra.cate = ReportAccess.findCate(c.getCateId());
//            r.listReport.add(ra);
//        }
//        for (ShowReportAction a : r.listReport) {
//            System.out.println(a.comp.getCreateTime() + "");
//        }

        for (Account t : ReportAccess.getAllAccount(4)) {
            System.out.println(t);
        }
    }

    private String time(long so) {
        if (so >= 10) {
            return so + "";
        } else {
            return "0" + so;
        }
    }

    public String addNewComplaint() {
        listCate = ReportAccess.getAllCategory();
        listDep = ReportAccess.getAllDeparment();
        listPri = ReportAccess.getAllPriority();
        if (comp != null) {
            comp.setAccId(6);
            comp.setCreateTime(new Date());
            listReport = getSummaryReport(ReportAccess.getAllComplaint());
            return ReportAccess.addNewComplaint(comp);
        } else {
            return "fail";
        }

    }

}
